import unittest
import uuid

from Autotests.BaseTest import BaseTestCase
from Autotests.Cloud.Clients import FinancialService


class InvoicersManagement(BaseTestCase):
    def test_InvoicersManagement(self):
        finserv = FinancialService()
        result = finserv.call('EnumerateInvoiceNumberSequences')

        if len(result.item) == 0:
            self.fail('EnumerateInvoiceNumberSequences returned empty list')
            return

        invoiceNumberSequenceId = result.item[0].id

        r = finserv.createObject('AddInvoicerRequest')
        r.invoicerInfo.name = 'test-' + str(uuid.uuid4())
        r.invoicerInfo.street = 'street'
        r.invoicerInfo.city = 'city'
        r.invoicerInfo.country = 'country'
        r.invoicerInfo.phone = 'phone'
        r.invoicerInfo.website = 'website'
        r.invoicerInfo.email = 'email'
        r.invoicerInfo.bankConnection = 'bankConnection'
        r.invoicerInfo.bankAccountNumber = 'bankAccountNumber'
        r.invoicerInfo.bicCode = 'bicCode'
        r.invoicerInfo.ibanCode = 'ibanCode'
        r.invoicerInfo.footerMessage = 'footerMessage'
        r.invoicerInfo.invoiceNumberSequenceId = invoiceNumberSequenceId

        newInvoicerId = finserv.call('AddInvoicer', r)

        r2 = finserv.createObject('GetInvoicerByIdRequest')
        r2.id = newInvoicerId

        invoicerInfo = finserv.call('GetInvoicerById', r2)

        def CompareInvoicerInfo(i1, i2):
            self.assertEqual(i1.name, i2.name)
            self.assertEqual(i1.street, i2.street)
            self.assertEqual(i1.city, i2.city)
            self.assertEqual(i1.country, i2.country)
            self.assertEqual(i1.phone, i2.phone)
            self.assertEqual(i1.website, i2.website)
            self.assertEqual(i1.email, i2.email)
            self.assertEqual(i1.bankConnection, i2.bankConnection)
            self.assertEqual(i1.bankAccountNumber, i2.bankAccountNumber)
            self.assertEqual(i1.bicCode, i2.bicCode)
            self.assertEqual(i1.ibanCode, i2.ibanCode)
            self.assertEqual(i1.footerMessage, i2.footerMessage)

        CompareInvoicerInfo(r.invoicerInfo, invoicerInfo)

        r = finserv.createObject('ModifyInvoicerRequest')
        r.invoicerInfo.id = newInvoicerId
        r.invoicerInfo.name = 'test-' + str(uuid.uuid4())
        r.invoicerInfo.street = 'street2'
        r.invoicerInfo.city = 'city2'
        r.invoicerInfo.country = 'country2'
        r.invoicerInfo.phone = 'phone2'
        r.invoicerInfo.website = 'website2'
        r.invoicerInfo.email = 'email2'
        r.invoicerInfo.bankConnection = 'bankConnection2'
        r.invoicerInfo.bankAccountNumber = 'bankAccountNumber2'
        r.invoicerInfo.bicCode = 'bicCode2'
        r.invoicerInfo.ibanCode = 'ibanCode2'
        r.invoicerInfo.footerMessage = 'footerMessage2'
        r.invoicerInfo.invoiceNumberSequenceId = invoiceNumberSequenceId

        finserv.call('ModifyInvoicer', r)

        invoicerInfo = finserv.call('GetInvoicerById', r2)

        CompareInvoicerInfo(r.invoicerInfo, invoicerInfo)

        enumResult = finserv.call('EnumerateInvoicers')
        self.assertGreaterEqual(len(enumResult.item), 1)

        if len(enumResult.item) >= 1:
            for i in enumResult.item:
                if i.id == newInvoicerId:
                    CompareInvoicerInfo(r.invoicerInfo, i)
                    break

        r = finserv.createObject('RemoveInvoicerRequest')
        r.id = newInvoicerId
        finserv.call('RemoveInvoicer', r)

if __name__ == "__main__":
    unittest.main()